package com.zyzd.general.entity.local;

import android.text.TextUtils;

import com.zyzd.commontools.utils.HanziToPinyin;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by lzy on 2018/5/16 0016.
 * description ${用户 Entity}
 */
public class UserEntity implements Serializable, Comparable<UserEntity> {
    private String user_name;
    private String phone;
    private int sex;
    private String describe;
    private int age;
    private String head_img_url;
    private String user_id;
    private String initialLetter;

    public UserEntity() {
    }

    public UserEntity(String user_name) {
        this.user_name = user_name;
    }

    public String getUserName() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getHead_img_url() {
        return head_img_url;
    }

    public void setHead_img_url(String head_img_url) {
        this.head_img_url = head_img_url;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getInitialLetter() {
        return initialLetter;
    }

    public void setInitialLetter(String initialLetter) {
        this.initialLetter = initialLetter;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "user_name='" + user_name + '\'' +
                ", phone='" + phone + '\'' +
                ", sex=" + sex +
                ", describe='" + describe + '\'' +
                ", age=" + age +
                ", head_img_url='" + head_img_url + '\'' +
                ", user_id='" + user_id + '\'' +
                ", initialLetter='" + initialLetter + '\'' +
                '}';
    }


    @Override
    public int compareTo(UserEntity o) {
        if (o.getInitialLetter().equals("#")) {
            return -1;
        }
        int i = getInitialLetter().compareTo(o.getInitialLetter());
        return i;
    }

    /**
     * set initial letter of according user's nickname( username if no nickname)
     *
     * @param user
     */
    public static void setUserInitialLetter(UserEntity user) {
        final String defaultLetter = "#";
        String letter = defaultLetter;

        final class GetInitialLetter {
            String getLetter(String name) {
                if (TextUtils.isEmpty(name)) {
                    return defaultLetter;
                }
                char char0 = name.toLowerCase().charAt(0);
                if (Character.isDigit(char0)) {
                    return defaultLetter;
                }
                ArrayList<HanziToPinyin.Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1));
                if (l != null && l.size() > 0 && l.get(0).target.length() > 0) {
                    HanziToPinyin.Token token = l.get(0);
                    String letter = token.target.substring(0, 1).toUpperCase();
                    char c = letter.charAt(0);
                    if (c < 'A' || c > 'Z') {
                        return defaultLetter;
                    }
                    return letter;
                }
                return defaultLetter;
            }
        }

        if (!TextUtils.isEmpty(user.getUserName())) {
            letter = new GetInitialLetter().getLetter(user.getUserName());
            user.setInitialLetter(letter);
            return;
        }
        if (letter.equals(defaultLetter) && !TextUtils.isEmpty(user.getUser_id())) {
            letter = new GetInitialLetter().getLetter(user.getUser_id());
        }
        user.setInitialLetter(letter);
    }
}
